package com.mti.app.view
{
	import com.mti.app.ApplicationFacade;
	import com.mti.app.model.business.QRBuilder;
	import com.mti.app.view.mycomponents.QRCodeView;
	import com.mti.app.view.mycomponents.QRitem;
	import com.mti.epivisite.common.event.EpivisiteEvent;
	import com.mti.epivisite.common.event.EventProxyBase;
	
	import flash.events.MouseEvent;
	
	import media.MediaPoint;
	
	import mx.collections.ArrayCollection;
	import mx.events.FlexEvent;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import spark.events.IndexChangeEvent;
	
	public class QRCodeViewMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "QRCodeViewMediator";
		private var evenement:EpivisiteEvent;
		
		private var infosMediator:InfosQRCodeViewMediator = null;
		
		[Bindable]
		public var eventProxy:EventProxyBase = null;
		
		public function QRCodeViewMediator(viewComponent:QRCodeView)
		{
			super(NAME, viewComponent);
			
			eventProxy = (facade.retrieveMediator(ApplicationMediator.NAME) as ApplicationMediator).serverEvent;
			
			view.mediator = this;
		}
		
		private function get view():QRCodeView
		{
			return viewComponent as QRCodeView;
		}
		
		override public function onRegister():void
		{
			super.onRegister();
			
			infosMediator = new InfosQRCodeViewMediator(view.infosView);
			facade.registerMediator(infosMediator);
			
			view.pointList.addEventListener(FlexEvent.VALUE_COMMIT, pointListChangeHandler);
			view.createPoiButton.addEventListener(MouseEvent.CLICK,createPoiButtonClickHandler); 
			view.deletePointButton.addEventListener(MouseEvent.CLICK, deletePoiButtonClickHandler);
		}
		
		private function pointListChangeHandler(event:FlexEvent):void
		{
			var selectedPoint:MediaPoint = view.pointList.selectedItem;
			infosMediator.mediaPoint = selectedPoint;
			view.currentState = (selectedPoint != null) ? QRCodeView.STATE_POINT_SELECTED : QRCodeView.STATE_NO_SELECTION;
		}
		
		private function createPoiButtonClickHandler(event:MouseEvent):void
		{
			var input:String = view.createPoiNameInput.text;
			if (input == "")
				return;
			var modEvent:EpivisiteEvent = eventProxy.modifiedEvent
			for each (var existingPoint:MediaPoint in modEvent.pointList)
			{
				if ((existingPoint.title == input)
					|| (existingPoint.QRCode == ("Epivisite-" + evenement.name + "-" + input)))
					return;
			}
			
			var createdPoint:MediaPoint = new MediaPoint();
			createdPoint.name = "Epivisite-" + modEvent.name + "-" + input;
			createdPoint.title = input;
			createdPoint.QRCode = "Epivisite-" + modEvent.name + "-" + input;
			createdPoint.description = input;
			createdPoint.shortDescription = input;
			createdPoint.eventId = modEvent.id;
			createdPoint.posX = 0;
			createdPoint.posY = 0;
			if (modEvent.pointList == null)
				modEvent.pointList = new ArrayCollection();
			modEvent.pointList.addItem(createdPoint);
			
			createdPoint.Visited = true; // Mark the point to be added to the database when saving.
			
			view.createPoiNameInput.text = "";
		}
		
		private function deletePoiButtonClickHandler(event:MouseEvent):void
		{
			var selectedPoint:MediaPoint = view.pointList.selectedItem;
			var modEvent:EpivisiteEvent = eventProxy.modifiedEvent;
			modEvent.pointList.removeItemAt(modEvent.pointList.getItemIndex(selectedPoint));
			modEvent.deletedPointList.addItem(selectedPoint);
			view.pointList.selectedIndex = -1;
		}
		
		override public function listNotificationInterests():Array
		{
			return [ApplicationFacade.LOADING_QRCODE_COMPLETE, EventProxyBase.GET_POINTS_SUCCESS, EventProxyBase.GET_POINTS_ERROR, ApplicationFacade.VIEW_INFOSQRCODE,  ApplicationFacade.VIEW_EVENEMENT];
		}
		
		override public function handleNotification(note:INotification):void
		{
			switch (note.getName())
			{
				case ApplicationFacade.LOADING_QRCODE_COMPLETE:
					/*if (!facade.hasMediator("evenementView"))
						facade.registerMediator(new EvenementViewMediator(qrcodeView.evenementView));*/
					/*if (!facade.hasMediator("infosqrcodeView"))
						facade.registerMediator(new InfosQRCodeViewMediator(qrcodeView.infosqrcodeView));*/
					sendNotification(ApplicationFacade.COMMAND_LOAD_EVENEMENT);
					sendNotification(ApplicationFacade.VIEW_EVENEMENT);
					evenement = eventProxy.modifiedEvent;
					//this.qrcodeView.AjouterButton.addEventListener(MouseEvent.CLICK, ajouterClickHandler);
					//this.qrcodeView.liste.removeAllElements();
					//this.qrcodeView.elemEvenement.addEventListener(MouseEvent.CLICK, eveClickHandler);

					eventProxy.getPoints(evenement);
					break;
				case EventProxyBase.GET_POINTS_SUCCESS:
					/*for each (var o:MediaPoint in evenement.pointList)
					{
						var qritem:QRitem = new QRitem();
						qritem.initialize();
						QRBuilder.initQR(qritem.bmp_qrCode);
						qritem.nameLabel.text = o.title;
						qritem.mediapoint = o;
						//QRBuilder.updateQR(o.QRCode, qritem.bmp_qrCode);
						qritem.test.addEventListener(MouseEvent.CLICK, modifiereClickHandler);
						
						this.view.addRow(qritem);
						
					}*/
					break;
				case ApplicationFacade.VIEW_INFOSQRCODE:
					/*this.qrcodeView.textEvenement.setStyle("color", "#ffffff");
					qrcodeView.qrcodeVS.selectedIndex = 2;*/
					break;
				case ApplicationFacade.VIEW_EVENEMENT:
					//qrcodeView.qrcodeVS.selectedIndex = 1;
					break;
			}
		}
		
		public function eveClickHandler(event:MouseEvent):void
		{
			/*sendNotification(ApplicationFacade.COMMAND_LOAD_EVENEMENT);
			sendNotification(ApplicationFacade.VIEW_EVENEMENT);
			this.qrcodeView.textEvenement.setStyle("color", "##7C7979");
			for (var j:int = 0; j < qrcodeView.liste.numChildren; j++) 
			{
				var i:QRitem = QRitem (qrcodeView.liste.getChildAt(j));
				i.supprimerButton.visible = false;
			}*/
/*			for each (var i:QRitem in qrcodeView.liste.getChildren()) 
			{
				i.supprimerButton.visible = false;
			}*/
		}


		public function modifiereClickHandler(event:MouseEvent):void
		{
			/*for (var j:int = 0; j < qrcodeView.liste.numChildren; j++) 
			{
				var i:QRitem = QRitem (qrcodeView.liste.getChildAt(j));
				i.supprimerButton.visible = false;
			}*/
/*			for each (var i:QRitem in qrcodeView.liste.getChildren()) 
			{
				i.supprimerButton.visible = false;
			}*/
			
			/*sendNotification(ApplicationFacade.COMMAND_LOAD_INFOSQRCODE, (QRitem (event.currentTarget.parent)).nameLabel.text);
			sendNotification(ApplicationFacade.VIEW_INFOSQRCODE);
			(QRitem (event.currentTarget.parent)).supprimerButton.visible = true;
			qrcodeView.qrcodeVS.visible = true;
			qrcodeView.RMMove.xFrom = qrcodeView.qrcodeVS.mouseX + 130;
			qrcodeView.RMMove.yFrom = qrcodeView.qrcodeVS.mouseY - 75;
			qrcodeView.resizeAndMove.play();
			qrcodeView.infosqrcodeView.viewSource = (QRitem (event.currentTarget.parent)).bmp_qrCode.source;
			qrcodeView.infosqrcodeView.mediapoint = (QRitem (event.currentTarget.parent)).mediapoint;*/
		}

	}
}